<?php
/*
 * @Description: 
 * @Date: 2022-08-21 17:17:10
 * @LastEditTime: 2022-08-21 18:02:20
 * @FilePath: \0821.la\upload_file.php
 */
session_start();
if($_SESSION['username']=='' && $_SESSION['id']==''){
    header("Location:/index.php");
    die;
}

include('config.php');
$allowedExts = array("gif", "jpeg", "jpg", "png","PNG");

$extension = end(explode(".", $_FILES["file"]["name"]));

if(
    $_FILES["file"]["type"]=="image/png"
    ||$_FILES["file"]["type"] == "image/jpeg"
    ||$_FILES["file"]["type"] == "image/jpg"
    ||$_FILES["file"]["type"] == "image/PNG"
    &&$_FILES["file"]["size"] < 20000
    &&in_array($extension, $allowedExts)
   ){
        $filename = explode('.',$_FILES["file"]["name"]);
        $sname = guid().'.'.$filename[1];
        move_uploaded_file($_FILES["file"]["tmp_name"],"./upload/" . $sname);
        $path = "/upload/". $sname;
        $sql = "UPDATE user  SET img =' $path' where id=".$_SESSION['id'];
        $result=$link->query($sql);
        if($result){
            $_SESSION['img'] = $path ;
            echo "<script>alert('头像上传成功')</script>";
            echo "<script>window.location.href='/main.php'</script>";
        }else{
            echo "<script>alert('头像上传失败')</script>";
            echo "<script>window.location.href='/main.php'</script>";
        }
        
}else{
    echo "<script>alert('无效的上传图片')</script>";
    echo "<script>window.location.href='/main.php'</script>";
}
function guid(){

   if (function_exists('com_create_guid')){

       return com_create_guid();

   }else{

       mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.

       echo(mt_rand());

       $charid = strtoupper(md5(uniqid(rand(), true)));

      

       $uuid =substr($charid, 0, 8).substr($charid, 8, 4)

               .substr($charid,12, 4)

               .substr($charid,16, 4)

               .substr($charid,20,12)

               ;// "}"

       return $uuid;

   }

}

?>